package com.android.pinprovince.view.group;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.pinprovince.R;
import com.android.pinprovince.activity.BaseObject;
import com.android.pinprovince.cache.DiskLruCacheHelper;
import com.android.pinprovince.fragment.Fragment1;
import com.android.pinprovince.model.group.MainSearchSortData;
import com.android.pinprovince.utils.ConstantsUrl;
import com.android.pinprovince.utils.GsonUtils;
import com.android.pinprovince.utils.ObjectUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class BannerItemFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private String mParam1;
    private int mParam2;
    private String mParam3;
    private List<MainSearchSortData.DataBean> mListSort = new ArrayList<>();


    public static BannerItemFragment newInstance(String thumb, int photolength, String link) {
        BannerItemFragment fragment = new BannerItemFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, thumb);
        args.putInt(ARG_PARAM2, photolength);
        args.putString(ARG_PARAM3, link);
        fragment.setArguments(args);
        return fragment;
    }

    public BannerItemFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getInt(ARG_PARAM2);
            mParam3 = getArguments().getString(ARG_PARAM3);


        }
        if(!TextUtils.isEmpty(mParam3)){
            if(mParam3.startsWith("/goods/index")){
                getListSort();
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.item_fragment_banner, null);
        ImageView iv = (ImageView) view.findViewById(R.id.imageView1);
        ObjectUtils.photo2(getActivity(), mParam1, iv);
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(mParam3)) {
                    if (mListSort.size() > 0) {
                        toNext();
                    } else {
                        if(mParam3.startsWith("http")){
                            Intent intent = new Intent(getActivity(), BaseObject.class);
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, "");
                            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_WEB_URL, mParam3);
                            startActivity(intent);
                        }
                    }
                }
            }
        });

        return view;
    }

    private void toNext() {
        if(!TextUtils.isEmpty(mParam3)){
            if(mParam3.startsWith("/goods/index")){
                String[] split = mParam3.split("/");
                String sortID = split[split.length - 1];
                for(int i = 0; i < mListSort.size(); i++){
                    MainSearchSortData.DataBean dataBean = mListSort.get(i);
                    if(dataBean!=null) {
                        if (sortID.equals(dataBean.getId())) {
                            Fragment1 instent = Fragment1.getInstent();
                            instent.setViewPager(i);
                        }
                    }
                }
            }
        }
    }

    private void getListSort() {
        DiskLruCacheHelper helper = null;
        try {
            helper = new DiskLruCacheHelper(getActivity());
        } catch (IOException e) {
            e.printStackTrace();
        }
        String cache_sort = helper.getAsString(ConstantsUrl.CACHE_HOME_SORTDATA_SEARCH);
        if (!TextUtils.isEmpty(cache_sort)) {
            MainSearchSortData response = GsonUtils.parseJSON(cache_sort, MainSearchSortData.class);
            mListSort.clear();
            List<MainSearchSortData.DataBean> data = response.getData();
            mListSort.add(new MainSearchSortData.DataBean("", "首页"));
            if (response.getData() != null && response.getData().size() > 0) {
                mListSort.addAll(data);
            }
        }
    }


}
